import { queryIncidence, queryReportItems, queryAlarmHandleRate, queryAlarmTotal, queryAlarmTrend } from './service';

export default {
  namespace: 'unit_report',

  state: {
    incidenceCurr: [],
    incidenceLast: [],
    fire: {},
    reportDetails: {}
  },

  effects: {
    *queryIncidence({ payload }, { call, put }) {
      const response = yield call(queryIncidence, payload);

      if (payload.month === new Date().getMonth()) {
        // console.log('curr');
        yield put({ type: 'setCurrentIncidence', payload: Array.isArray(response) ? response : [] });
      } else {
        // console.log('last');
        yield put({ type: 'setLastIncidence', payload: Array.isArray(response) ? response : [] });
      }
    },
    *queryReportItems(_, { call, put }) {
      const response = yield call(queryReportItems);
      yield put({ type: 'setReportDetails', payload: response });
    },
    *queryAlarmHandleRate({ payload }, { call, put }) {
      const response = yield call(queryAlarmHandleRate, payload);
      yield put({ type: 'setAlarmHandleRate', payload: response });
    },
    //设备状态报警占比
    *queryAlarmTotal({ payload }, { call, put }) {
      const response = yield call(queryAlarmTotal, payload);
      yield put({ type: 'setAlarmTotal', payload: response });
    },
    //警报趋势
    *queryAlarmTrend({ payload }, { call, put }) {
      const response = yield call(queryAlarmTrend, payload);
      yield put({ type: 'setAlarmTrend', payload: response });
    },
  },

  reducers: {
    setAlarmTrend(state, action) {
      return {
        ...state,
        alarmTrend: action.payload
      };
    },
    setAlarmHandleRate(state, action) {
      return {
        ...state,
        alarmHandleRate: action.payload
      };
    },
    setAlarmTotal(state, action) {
      return {
        ...state,
        alarmTotal: action.payload
      };
    },

    setAlarmToday(state, action) {
      return {
        ...state,
        alarmToday: action.payload
      };
    },

    setCurrentIncidence(state, action) {
      return {
        ...state,
        incidenceCurr: action.payload
      };
    },
    setLastIncidence(state, action) {
      return {
        ...state,
        incidenceLast: action.payload
      };
    },

    setReportDetails(state, action) {
      return {
        ...state,
        reportDetails: action.payload
      };
    }
  }
};
